//导入tab模块
import {Tab} from './tab.js';
//导入cookie模块
import {removeCookie,getCookie,setCookie} from './cookie.js'
//导入ajax模块
import {ajax,isObject} from './ajax.js'
//实例化一个登录表单的tab选项卡*****************************
$(".importFooter").load("./footer1.html")
//获取登录表单
var loginForm = document.getElementsByClassName('loginForm')[0];
//获取选择区
var Taboption = loginForm.getElementsByClassName('Taboption');
//获取展示区
var Tabview = loginForm.getElementsByClassName('Tabview');
//创建选择区选中样式
var obj1 = {
    opacity: '1',
    fontWeight: 'bold',
    borderBottom: '3px solid #e4393c'
}
//创建选择区未选中样式
var obj2 = {
    opacity: '.65',
    fontWeight: 'normal',
    borderBottom: '3px solid #fff'
}
//实例化tab选项卡
new Tab(loginForm, Taboption, Tabview, obj1, obj2)

//实例化一个登录表单的tab选项卡*****************************

//判断是否已存在cookies****************************        
if (getCookie("password")) {
    document.getElementsByClassName("divPassLogin")[0].getElementsByTagName("input")[0].value = getCookie("username")
    document.getElementsByClassName("divPassLogin")[0].getElementsByTagName("input")[1].value = getCookie("password")
    document.getElementsByClassName('passwordAbout')[0].children[0].checked = true;
}
//判断是否已存在cookies****************************

//创建获取验证码事件******************************
//获取按钮
var noteCode = document.getElementsByClassName('noteCode')[0];
//绑定点击事件
noteCode.onclick = function () {
    //获取账号输入框
    loginInput = document.getElementsByClassName("divnoteLogin")[0].getElementsByTagName("input")[0];
    //创建ajax参数对象
    var ajaxobj = {
        url: './php/login.php',
        type: 'get',
        dataType: 'json',
        data: {
            username: loginInput.value,
            type: 'noteCode'
        },
        success: function (a) {
            noteCode.innerHTML = a.msg
        },
        error: function (a) {
            console.log('网络异常')
        }
    }
    //执行ajax函数
    ajax(ajaxobj)
}
//创建获取验证码事件******************************

//登录按钮绑定点击事件****************************
//获取登录按钮
var loginButton = document.getElementsByClassName("loginButton")[0];
//获取账号输入框
var loginInput;
//获取密码输入框
var passInput;
//定义是否清除cooki
var clCookie = false;

//绑定登录按钮
loginButton.onclick = function () {
    //判断是密码登录还是短信登录

    //当密码登录框显示时获取密码登录框下的输入框
    if (document.getElementsByClassName("divPassLogin")[0].style.display === 'block') {
        //获取账号输入框
        loginInput = document.getElementsByClassName("divPassLogin")[0].getElementsByTagName("input")[0];
        //获取密码输入框
        passInput = document.getElementsByClassName("divPassLogin")[0].getElementsByTagName("input")[1];
        //设定是否清除cooki
        clCookie = true;
        //获取是否记住密码
        var reb = document.getElementsByClassName('passwordAbout')[0].children[0].checked;
       
        if (reb) {
            setCookie({
                key: "username",
                val: loginInput.value,
                days: 7
            })
            setCookie({
                key: "password",
                val: passInput.value,
                days: 7
            })
        } else {
            removeCookie("password")
        }
        //当密码登录框隐藏时获取短信登录框下的输入框
    } else if (document.getElementsByClassName("divPassLogin")[0].style.display === 'none') {
        //获取账号输入框
        loginInput = document.getElementsByClassName("divnoteLogin")[0].getElementsByTagName("input")[0];
        //获取密码输入框
        passInput = document.getElementsByClassName("divnoteLogin")[0].getElementsByTagName("input")[1];
        //设定是否清除cooki
        clCookie = false;
    }

    //创建ajax参数对象
    var ajaxobj = {
        url: './php/login.php',
        type: 'get',
        dataType: 'json',
        data: {},
        success: function (a) {
            //判断是否记住密码
            if ((a.msg === "账号或密码错误") && clCookie) {
                //只在密码登录失败的情况下删除cookie
                removeCookie("password")
            } else if ((a.msg === "登录成功") && !clCookie) {
                //短信登录成功后改变验证码
                //获取账号输入框
                loginInput = document.getElementsByClassName("divnoteLogin")[0].getElementsByTagName("input")[0];
                //创建ajax参数对象
                var ajaxobj = {
                    url: './php/login.php',
                    type: 'get',
                    dataType: 'json',
                    data: {
                        username: loginInput.value,
                        type: 'noteCode'
                    },
                    success: function (a) {

                    },
                    error: function (a) {
                        console.log('网络异常')
                    }
                }
                //执行ajax函数
                ajax(ajaxobj)
            }
            if(a.msg === "登录成功"){
                document.cookie="username="+loginInput.value;
                document.cookie="loginsucess=true";
                alert("登录成功，即将跳转到首页")
                window.location.href="./index.html";
            }else{
                alert('账号或密码错误')
            }
        },
        error: function (a) {
            console.log('网络异常')
        }
    }

    //获取账号输入框中的值，并加入ajaxobj.data中
    ajaxobj.data.username = loginInput.value;
    //获取密码输入框中的值，并加入ajaxobj.data中
    ajaxobj.data.password = passInput.value;
    //将请求登录加入
    ajaxobj.data.type = "login";
    //调用ajax函数
    ajax(ajaxobj);
}

//登录按钮绑定点击事件****************************

//密码显示与否************************************
var see1 = document.getElementsByClassName('see')[0];
see1.setAttribute("font1", "&#xe61b;")
see1.onclick = function () {

    if (see1.getAttribute("font1") == '&#xe61b;') {
        see1.innerHTML = '&#xe8c7;'
        see1.setAttribute("font1", "&#xe8c7;")
        document.getElementsByClassName("divPassLogin")[0].getElementsByTagName("input")[1].type = "text"
    } else if (see1.getAttribute("font1") == '&#xe8c7;') {
        see1.innerHTML = '&#xe61b;'
        see1.setAttribute("font1", "&#xe61b;")
        document.getElementsByClassName("divPassLogin")[0].getElementsByTagName("input")[1].type = "password"
    }
}
//密码显示与否************************************
